// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
#pragma once

#include <pollux/common/base/compare_flags.h>
#include <pollux/functions/macros.h>
#include <pollux/type/floating_point_util.h>

namespace kumo::pollux::functions {
#define POLLUX_GEN_BINARY_EXPR(Name, Expr, ExprForFloats)       \
  template <typename T>                                        \
  struct Name {                                                \
    POLLUX_DEFINE_FUNCTION_TYPES(T);                            \
    template <typename TInput>                                 \
    MELON_ALWAYS_INLINE void                                   \
    call(bool& result, const TInput& lhs, const TInput& rhs) { \
      if constexpr (std::is_floating_point_v<TInput>) {        \
        result = (ExprForFloats);                              \
        return;                                                \
      }                                                        \
      result = (Expr);                                         \
    }                                                          \
  };

    POLLUX_GEN_BINARY_EXPR(
        LtFunction,
        lhs < rhs,
        util::floating_point::NaNAwareLessThan<TInput>{}(lhs, rhs));

    POLLUX_GEN_BINARY_EXPR(
        GtFunction,
        lhs > rhs,
        util::floating_point::NaNAwareGreaterThan<TInput>{}(lhs, rhs));

    POLLUX_GEN_BINARY_EXPR(
        LteFunction,
        lhs <= rhs,
        util::floating_point::NaNAwareLessThanEqual<TInput>{}(lhs, rhs));

    POLLUX_GEN_BINARY_EXPR(
        GteFunction,
        lhs >= rhs,
        util::floating_point::NaNAwareGreaterThanEqual<TInput>{}(lhs, rhs));

#undef POLLUX_GEN_BINARY_EXPR
#undef POLLUX_GEN_BINARY_EXPR_TIMESTAMP_WITH_TIME_ZONE

    template<typename TExec>
    struct DistinctFromFunction {
        POLLUX_DEFINE_FUNCTION_TYPES(TExec);

        void callNullable(
            bool &result,
            const arg_type<Generic<T1> > *lhs,
            const arg_type<Generic<T1> > *rhs) {
            if (!lhs and !rhs) {
                // Both nulls -> not distinct.
                result = false;
            } else if (!lhs or !rhs) {
                // Only one is null -> distinct.
                result = true;
            } else {
                // Both not nulls - use usual comparison.
                static constexpr CompareFlags kCompareFlags =
                        CompareFlags::equality(CompareFlags::NullHandlingMode::kNullAsValue);

                result = lhs->compare(*rhs, kCompareFlags).value();
            }
        }
    };

    template<typename T>
    struct EqFunction {
        POLLUX_DEFINE_FUNCTION_TYPES(T);

        // Used for primitive inputs.
        template<typename TInput>
        void call(bool &out, const TInput &lhs, const TInput &rhs) {
            if constexpr (std::is_floating_point_v<TInput>) {
                out = util::floating_point::NaNAwareEquals<TInput>{}(lhs, rhs);
                return;
            }
            out = (lhs == rhs);
        }

        // For arbitrary nested complex types. Can return null.
        bool call(
            bool &out,
            const arg_type<Generic<T1> > &lhs,
            const arg_type<Generic<T1> > &rhs) {
            static constexpr CompareFlags kFlags = CompareFlags::equality(
                CompareFlags::NullHandlingMode::kNullAsIndeterminate);

            auto result = lhs.compare(rhs, kFlags);
            if (!result.has_value()) {
                return false;
            }
            out = (result.value() == 0);
            return true;
        }
    };

    template<typename T>
    struct NeqFunction {
        POLLUX_DEFINE_FUNCTION_TYPES(T);

        // Used for primitive inputs.
        template<typename TInput>
        void call(bool &out, const TInput &lhs, const TInput &rhs) {
            if constexpr (std::is_floating_point_v<TInput>) {
                out = !util::floating_point::NaNAwareEquals<TInput>{}(lhs, rhs);
                return;
            }
            out = (lhs != rhs);
        }

        // For arbitrary nested complex types. Can return null.
        bool call(
            bool &out,
            const arg_type<Generic<T1> > &lhs,
            const arg_type<Generic<T1> > &rhs) {
            if (EqFunction<T>().call(out, lhs, rhs)) {
                out = !out;
                return true;
            } else {
                return false;
            }
        }
    };

    template<typename TExec>
    struct BetweenFunction {
        template<typename T>
        MELON_ALWAYS_INLINE void
        call(bool &result, const T &value, const T &low, const T &high) {
            if constexpr (std::is_floating_point_v<T>) {
                result =
                        util::floating_point::NaNAwareGreaterThanEqual<T>{}(value, low) &&
                        util::floating_point::NaNAwareLessThanEqual<T>{}(value, high);
                return;
            }
            result = value >= low && value <= high;
        }
    };
} // namespace kumo::pollux::functions
